<template>
  <div class="app-container">
    <el-dialog title="选择赠品" :visible.sync="visible" width="800px" append-to-body @close="toCancelBox">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px"
        @keyup.enter.native="handleQuery">
        <el-row>
          <el-form-item label="赠品名称" prop="giftName">
            <el-input v-model="queryParams.giftName" placeholder="请输入赠品名称" clearable size="small" class="input-rule" />
          </el-form-item>
          <el-form-item style="margin-left: 50px;">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>

      <!-- <el-row :gutter="10" class="row-mb2">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" :disabled="single">添加
          </el-button>
        </el-col>
      </el-row> -->

      <el-table border v-loading="loading"  ref="indexTable" :data="infoList" @selection-change="handleSelectionChange" :row-key="bindRowKeys" tooltip-effect="dark">
        <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
        <el-table-column label="赠品图片" align="center" prop="proImgOne" width="100">
          <template slot-scope="scope">
            <el-image :z-index="zIndex" v-if="scope.row.proImgOne != null && scope.row.proImgOne != '' "
              slot="reference" :src="scope.row.proImgOne" class="image-rows"
              :preview-src-list="[scope.row.proImgOne]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="赠品名称" align="center" prop="giftName" :show-overflow-tooltip="true"/>
        <el-table-column label="赠品总数" align="center" prop="stockNumber" sortable  />
        <el-table-column label="领取次数" align="center" prop="receiveNumber" sortable  />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="200" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span><br />
            <span>{{ statusFormat(scope.row.dataStatus) }}</span>
          </template>
        </el-table-column>

      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList"  :page-sizes="pageSizess"/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="determine" type="primary">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGift,
    getGift,
    delGift,
    addGift,
    updateGift
  } from "@/api/activity/gift";

  export default {
    name: "gift",
    props: {
      flag: {
        type: Boolean,
      },
    },
    data() {
      return {
        serviceTypesList:[],
        pageSizess: [5, 10, 15, 20, 25, 30, 50],
        zIndex: 3000,
        // 遮罩层
        loading: true,

        datas: null,
        //已选数据
        selectedDatas: [],

        // 赠品信息表格数据
        infoList: [],

        infoListHistory: [],
        multipleSelection: [],

        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          giftName: null,
          dataStatus: '1',
        },
        // 表单参数
        form: {},
        visible: false,
      };
    },
    created() {
      this.visible = this.flag;

      this.getList();
    },
    methods: {
      toCancelBox() {
        this.visible = false;
        this.$emit("toCloseBox", false);
      },
      greet(selectedData) {
        this.selectedDatas = selectedData;
        this.selectedDatas.forEach((item) => {
          this.$refs.indexTable.toggleRowSelection(item, true);
        });
      },
      bindRowKeys(row) {
        return row.giftUid;
      },
      handleSelectionChange(val) {
        this.datas = this.multipleSelection;
        this.multipleSelection = val
        if (this.datas.length > 0) {
          for (let i = 0; i < this.datas.length; i++) {
            for (let k = 0; k < this.multipleSelection.length; k++) {
              if (this.datas[i].giftUid == this.multipleSelection[k].giftUid) {
                this.multipleSelection[k] = this.datas[i]
              }
            }
          }
        }
      },
      determine() {
        this.$emit("multipleSelection", this.multipleSelection);
        this.infoList.forEach((item) => {
          this.$refs.indexTable.toggleRowSelection(item, false);
        });
        this.$forceUpdate();
        this.open = false;
      },
      /** 查询赠品信息列表 */
      getList() {
        this.loading = true;

        listGift(this.queryParams).then((response) => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      statusFormat(status) {
        let showStatus = "";
        if (status == "1") {
          showStatus = "有效";
        } else {
          showStatus = "无效"
        }
        return showStatus;
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      }
    }
  };
</script>

<style scoped>
  .input-rule {
    width: 232.45px;
  }

  .row-mb2 {
    margin: -7.5px -7.5px 7.5px -7.5px;
  }
</style>
